package Demo1;


public class DoublyLinkedList {


    static class LinkedNode {
        private int val;
        private LinkedNode prior;
        private LinkedNode next;

        public LinkedNode(int val) {
            this.val = val;
            this.prior = null;
            this.next = null;
        }
    }

    public LinkedNode head;// 双向链表的头结点
    public LinkedNode last;// 双向链表的尾结点


    //头插法
    public void addFirst(int data){
       LinkedNode newNode = new LinkedNode(data);
        if (head == null) {
            head = newNode;
            last = newNode;
        } else {
            newNode.next = head;
            head.prior = newNode;
            head = newNode;
        }
    }

    //尾插法
    public void addLast(int data){
        LinkedNode newNode = new LinkedNode(data);
        if (head == null){
            head = newNode;
            last = newNode;
        }else {
            newNode.prior = last;
            last.next = newNode;
            last = last.next;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if (index < 0 || index > size()){
            System.out.println("非法下标");
        } else if (index == 0) {
            addFirst(data);
            return;
        } else if (index == size()) {
            addLast(data);
            return;
        }else {
            LinkedNode node = new LinkedNode(data);
            LinkedNode cur = head;
            for (int i = 0; i < index-1; i++) {
                cur = cur.next;
            }
            // 此时 cur 是插入位置的前一个节点
            LinkedNode temp = cur.next;
            node.next = temp;
            node.prior = cur;
            cur.next = node;
            temp.prior = node;
        }
    }


    //删除第一次出现关键字为key的节点
    public void remove(int key){
        LinkedNode cur = head;
        while(cur != null){
            if (cur.val == key){
                if (cur.prior == null){
//                    删除头节点
                   head = cur.next;
                   if (head != null){
                       head.prior = null;
                   }else {
                       last = null;
                   }
                } else if (cur.next == null) {
//                    删除尾节点
                    cur.prior.next = null;
                    last = cur.prior;
                    break;
                }else {
                    LinkedNode temp = cur.prior;
                    temp.next = cur.next;
                    cur.next.prior = temp;
                    break;
                }
            }
            cur = cur.next;
        }

    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        LinkedNode cur = head;
        while(cur != null){
            if (cur.prior == null){
//                删除头节点
                head = cur.next;
                if (head != null){
                    head.prior = null;
                }else {
                    last = null;//链表为空
                }
            } else if (cur.next == null) {
//                删除尾结点
                last = cur.prior;
                cur.prior.next = null;
            }else {
//                删除中间节点
               cur.prior.next = cur.next;
               cur.next.prior = cur.prior;
            }
            cur = cur.next;
        }
    }

    //查找是否包含关键字key是否在链表当中
    public boolean contains(int key){
        LinkedNode cur = head;
        while(cur.next != null){
            if (cur.val == key){
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到双向链表的长度
    public int size(){
        int count = 0;
        LinkedNode cur = head;
        while(cur != null){
            count++;
            cur = cur.next;
        }
        return count;
    }

//    打印链表
    public void display(){
        LinkedNode cur = head;
        while(cur != null){
            System.out.print(cur.val+" ");
            cur =cur.next;
        }
        System.out.println();
    }

//    清空
    public void clear(){
        LinkedNode cur = head;
        while(cur != null){
            cur.next = null;
            cur.prior = null;
            cur = cur.next;
        }
        head = null;
        last = null;
    }

}
